package cn.iocoder.yudao.module.asasc.controller.admin.campaign;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.asasc.controller.admin.campaign.vo.CampaignDataPageReqVO;
import cn.iocoder.yudao.module.asasc.controller.admin.campaign.vo.CampaignDataRespVO;
import cn.iocoder.yudao.module.asasc.controller.admin.campaign.vo.CampaignDataSaveReqVO;
import cn.iocoder.yudao.module.asasc.dal.dataobject.campaign.CampaignDataDO;
import cn.iocoder.yudao.module.asasc.service.campaign.CampaignDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 广告数据")
@RestController
@RequestMapping("/asasc/campaign-data")
@Validated
public class CampaignDataController {

    @Resource
    private CampaignDataService campaignDataService;

    @PostMapping("/create")
    @Operation(summary = "创建广告数据")
    @PreAuthorize("@ss.hasPermission('asasc:campaign-data:create')")
    public CommonResult<Long> createCampaignData(@Valid @RequestBody CampaignDataSaveReqVO createReqVO) {
        return success(campaignDataService.createCampaignData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告数据")
    @PreAuthorize("@ss.hasPermission('asasc:campaign-data:update')")
    public CommonResult<Boolean> updateCampaignData(@Valid @RequestBody CampaignDataSaveReqVO updateReqVO) {
        campaignDataService.updateCampaignData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asasc:campaign-data:delete')")
    public CommonResult<Boolean> deleteCampaignData(@RequestParam("id") Long id) {
        campaignDataService.deleteCampaignData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('asasc:campaign-data:query')")
    public CommonResult<CampaignDataRespVO> getCampaignData(@RequestParam("id") Long id) {
        CampaignDataDO campaignData = campaignDataService.getCampaignData(id);
        return success(BeanUtils.toBean(campaignData, CampaignDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告数据分页")
    @PreAuthorize("@ss.hasPermission('asasc:campaign-data:query')")
    public CommonResult<PageResult<CampaignDataRespVO>> getCampaignDataPage(@Valid CampaignDataPageReqVO pageReqVO) {
        PageResult<CampaignDataDO> pageResult = campaignDataService.getCampaignDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CampaignDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告数据 Excel")
    @PreAuthorize("@ss.hasPermission('asasc:campaign-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCampaignDataExcel(@Valid CampaignDataPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CampaignDataDO> list = campaignDataService.getCampaignDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告数据.xls", "数据", CampaignDataRespVO.class,
                BeanUtils.toBean(list, CampaignDataRespVO.class));
    }

}